#include "addfriendreceiver.h"
#include "ui_addfriendreceiver.h"
QVariantMap pullChatMap;
addfriendreceiver::addfriendreceiver(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::addfriendreceiver)
{
    ui->setupUi(this);
    //connect()
    qDebug() << "userSelf:" << userSelf.getUserName();
    //websocket传输代码
    this->userSelf = userSelf;
    socket = new QWebSocket();//新建websocket
    QUrl url("ws://101.43.168.188:19999/slimarveous/ws");
    QUrlQuery query;//在query里面添加id token两个信息
    query.addQueryItem("id",userSelf.getUserId());
    query.addQueryItem("token",userSelf.getUserPassword());
    url.setQuery(query);
    socket->open(url);
    //连接到server
    QObject::connect(socket, &QWebSocket::connected,[=]() {
        qDebug() << "WebSocket connected";
    });
    //断开链接
    QObject::connect(socket, &QWebSocket::disconnected, [=]() {
        qDebug() << "WebSocket disconnected";
    });
    //获取信息
    QObject::connect(socket, &QWebSocket::textMessageReceived, [=](const QString &message) {
        qDebug() << "Received message:" << message;
        //接收信息
        QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
        //doc转换成json
        QJsonObject obj = doc.object();
        //分析接收到的服务器信息类型
        QString type = obj.value("type").toString();//传到服务器后端的类型type参数
        if (type == "AddFriendReq")//未完成
        {
            //将未读消息json文件转化成map
            pullChatMap = obj.toVariantMap();//转化为variantMap
            //QJsonObject ucJsonObj = pullChatMap.toObject();
                //qDebug() << "这里是未读信息： " << ucJsonObj;
               // QVariantMap messageMap = ucJsonObj.toVariantMap();
            QMap senderMap = pullChatMap.value("Sender").toMap();
            QVariant id = senderMap.value("Id");
            QVariant gender = pullChatMap.value("Gender");
            QString brief=pullChatMap.value("Brief").toString();
            ui->senderid->append(id.toString());//对话框中添加id
            ui->sendergender->append(gender.toString());//对话框中添加gender
            ui->senderbrief->append(brief);//对话框中添加brief
        }
    });
    connect(ui->accept,&QPushButton::clicked,this, &addfriendreceiver:: accept_clicked);
    connect(ui->refuse,&QPushButton::clicked,this, &addfriendreceiver:: refuse_clicked);
}

addfriendreceiver::~addfriendreceiver()
{
    delete ui;
}
void addfriendreceiver:: accept_clicked()
{
    int IsAcceptedCode=2;
    UserBrief Self;
    Self = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    QMap senderMap = pullChatMap.value("Sender").toMap();
    QVariant id = senderMap.value("Id");
    QVariant username = senderMap.value("Username");
    QVariant gender = senderMap.value("gender");
    UserBrief TheOther;
    TheOther ={
        id.toString(),
        username.toString(),
        gender.toString()
    };
//    qDebug() << "sender信息:" << Sender.username + Sender.id;
    //get content
    //QString Brief = ui->messageSendTextEdit->toPlainText();//传输服务器
    //QString senderToken = ui->senderToken->text();
    //QString receiverId = ui->receiverId->text();
    //receiverId.toLongLong();//id转化为long long
    //connect to server

    //sender信息
    QJsonObject senderValue{
        {"id", Self.id.toInt()},
        {"username", Self.username},
        {"photo", ""},
        {"gender", Self.gender.toInt()}
    };
    QJsonObject receiverValue{
        {"id", TheOther.id.toInt()},
        {"username", TheOther.username},
        {"photo", ""},
        {"gender", TheOther.gender.toInt()}
    };
    //发送的信息
    QJsonObject json{
        {"IsAcceptedCode",IsAcceptedCode},
        {"Self",senderValue},
        {"TheOther",receiverValue},
        {"Type", "add_friend_fail_or_success"}
    };

    QJsonDocument doc(json);

    QByteArray data = doc.toJson(QJsonDocument::Compact);

    socket->sendTextMessage(data);
}
void addfriendreceiver:: refuse_clicked()
{
    int IsAcceptedCode=1;
    UserBrief Self;
    Self = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    QMap senderMap = pullChatMap.value("Sender").toMap();
    QVariant id = senderMap.value("Id");
    QVariant username = senderMap.value("Username");
    QVariant gender = senderMap.value("gender");
    UserBrief TheOther;
    TheOther ={
        id.toString(),
        username.toString(),
        gender.toString()
    };
//    qDebug() << "sender信息:" << Sender.username + Sender.id;
    //get content
    //QString Brief = ui->messageSendTextEdit->toPlainText();//传输服务器
    //QString senderToken = ui->senderToken->text();
    //QString receiverId = ui->receiverId->text();
    //receiverId.toLongLong();//id转化为long long
    //connect to server

    //sender信息
    QJsonObject senderValue{
        {"id", Self.id.toInt()},
        {"username", Self.username},
        {"photo", ""},
        {"gender", Self.gender.toInt()}
    };
    QJsonObject receiverValue{
        {"id", TheOther.id.toInt()},
        {"username", TheOther.username},
        {"photo", ""},
        {"gender", TheOther.gender.toInt()}
    };
    //发送的信息
    QJsonObject json{
        {"IsAcceptedCode",IsAcceptedCode},
        {"Self",senderValue},
        {"TheOther",receiverValue},
        {"Type", "CheckFriendMsg"}
    };

    QJsonDocument doc(json);

    QByteArray data = doc.toJson(QJsonDocument::Compact);

    socket->sendTextMessage(data);
}
